<?php

class SiteController extends Controller
{

	public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules()
    {
        return array(

        	array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('login','logout','upload','mm'),
                'users'=>array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('index'),
                'users'=>array('admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			// 'captcha'=>array(
			// 	'class'=>'CCaptchaAction',
			// 	'backColor'=>0xFFFFFF,
			// ),
			// // page action renders "static" pages stored under 'protected/views/site/pages'
			// // They can be accessed via: index.php?r=site/page&view=FileName
			// 'page'=>array(
			// 	'class'=>'CViewAction',
			// ),
		);
	}

	public function actionMm()
	{
		$a = 'ccc';
		// echo $a[0];
		// echo "hello";
		// Yii::app()->setLanguage('en_db');
		echo Yii::t('message', 'name');
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		//$this->redirect($this->createUrl("adManage/index"));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	public function actionUpload()
	{
		$upload_handler = new UploadHandler();
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
				$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
				$headers="From: $name <{$model->email}>\r\n".
					"Reply-To: {$model->email}\r\n".
					"MIME-Version: 1.0\r\n".
					"Content-Type: text/plain; charset=UTF-8";

				mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		if($_POST['username'] == 'admin'&&$_POST['password']."" == 'spcva8'){
			Yii::app()->user->login(new UserIdentity('admin','spcva8'),30*10000);
		}else{
			header("HTTP/1.0 401 Not Found");
		}
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}